<pre>

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import bingosoft.components.base.helper.ObjectHelper;
import bingosoft.components.base.service.ActionService;
import bingosoft.components.base.service.PageQueryRequest;
import bingosoft.components.base.service.Result;
import bingosoft.components.base.service.ResultEntity;
import bingosoft.components.base.service.ResultPage;
import bingosoft.metro.variable.VariableFactory;
import leap.core.exception.RecordNotDeletedException;
import leap.core.exception.RecordNotFoundException;
import leap.core.exception.RecordNotSavedException;
import leap.lang.Strings;
import leap.orm.query.PageResult;
import leap.orm.query.Query;

/**
 * @author ${author}
 * @功能描述 ${table.tableComment}业务服务接口实现类
 * @创建时间 ${date}
 */
public class ${table.capitalTableName}Service extends ActionService implements I${table.capitalTableName}Service {

	public static final String MESSAGE_PARAM_OBJ_NULL = "参数【${table.humpTableName}Model】不能为null！";
	public static final String MESSAGE_PARAM_ID_NULL = "参数【${pkCol.columnName}】不能为空！";
	public static final String MESSAGE_QUERY_FAIL = "查询失败！";
	public static final String MESSAGE_CREATE_SUCCESS = "创建成功！";
	public static final String MESSAGE_CREATE_FAIL = "创建失败（没有数据被创建）！";
	public static final String MESSAGE_UPDATE_SUCCESS = "修改成功！";
	public static final String MESSAGE_UPDATE_FAIL = "修改失败（ID不存在或没有数据被修改）！";
	public static final String MESSAGE_GET_SUCCESS = "获取成功！";
	public static final String MESSAGE_GET_FAIL = "获取失败（ID不存在）！";
	public static final String MESSAGE_DELETE_SUCCESS = "删除成功！";
	public static final String MESSAGE_DELETE_FAIL = "删除失败（没有数据被删除）！";
	
	/**
	 * 构建函数
	 */
	public ${table.capitalTableName}Service(){
		this.setLogClass(this.getClass());
	}
	
	@Override
	public ResultEntity&lt;List&lt;${table.capitalTableName}Form&gt;&gt; query${table.capitalTableName}s(PageQueryRequest pageQueryRequest, Map&lt;String, Object&gt; params) {
		this.logMethodCalled("query${table.capitalTableName}s");
		this.logParamValue("pageQueryRequest", pageQueryRequest);
		this.logParamValue("params", params);
		
		if(pageQueryRequest == null){
			pageQueryRequest = new PageQueryRequest();
		}
		
		// 根据sqlKey构建查询SQL
		String selectSql = this.buildSelectSql("");
		pageQueryRequest.mergeFilters(selectSql, params);

		// 构建查询对象
		Query&lt;${table.capitalTableName}Model&gt; query = ${table.capitalTableName}Model.&lt;${table.capitalTableName}Model&gt;query(pageQueryRequest.getMergeSql()).params(pageQueryRequest.getParameter());
		if(!Strings.isEmpty(pageQueryRequest.getSort())){
			query = query.orderBy(pageQueryRequest.getSort());
		}

		// 返回查询结果
		ResultEntity&lt;List&lt;${table.capitalTableName}Form&gt;&gt; resultEntity = new ResultEntity&lt;List&lt;${table.capitalTableName}Form&gt;&gt;();
		try {
			List&lt;${table.capitalTableName}Model&gt; ${table.humpTableName}Models = query.list();
			// 构建表单类
			List&lt;${table.capitalTableName}Form&gt; ${table.humpTableName}Forms = this.build${table.capitalTableName}Forms(${table.humpTableName}Models);
			
			resultEntity.setBusinessObject(${table.humpTableName}Forms);
		} catch (Exception ex) {
			this.logError(MESSAGE_QUERY_FAIL, ex);
			resultEntity.setSuccess(false);
			resultEntity.setMessage(Strings.format("{0}[{1}]", MESSAGE_QUERY_FAIL,ex.getMessage()));
		}
		return resultEntity;
	}

	@Override
	public ResultEntity&lt;ResultPage&lt;List&lt;${table.capitalTableName}Form&gt;&gt;&gt; pageQuery${table.capitalTableName}s(PageQueryRequest pageQueryRequest, Map&lt;String, Object&gt; params) {
		this.logMethodCalled("pageQuery${table.capitalTableName}s");
		this.logParamValue("pageQueryRequest", pageQueryRequest);
		this.logParamValue("params", params);

		if(pageQueryRequest == null){
			pageQueryRequest = new PageQueryRequest();
		}
		
		int pageIndex = pageQueryRequest.getIndex();
		int pageSize = pageQueryRequest.getSize();
		this.logParamValue("pageIndex", pageIndex);
		this.logParamValue("pageSize", pageSize);
		
		// 根据sqlKey构建查询SQL
		String selectSql = this.buildSelectSql("");
		pageQueryRequest.mergeFilters(selectSql, params);

		// 构建查询对象
		Query&lt;${table.capitalTableName}Model&gt; query = ${table.capitalTableName}Model.&lt;${table.capitalTableName}Model&gt;query(pageQueryRequest.getMergeSql()).params(pageQueryRequest.getParameter());
		if(!Strings.isEmpty(pageQueryRequest.getSort())){
			query = query.orderBy(pageQueryRequest.getSort());
		}
		
		// 返回查询结果
		ResultEntity&lt;ResultPage&lt;List&lt;${table.capitalTableName}Form&gt;&gt;&gt; resultEntity = new ResultEntity&lt;ResultPage&lt;List&lt;${table.capitalTableName}Form&gt;&gt;&gt;();
		try {
			PageResult&lt;${table.capitalTableName}Model&gt; pageResult = query.pageResult(pageIndex, pageSize);
			List&lt;${table.capitalTableName}Model&gt; ${table.humpTableName}Models = pageResult.list();
			
			// 构建表单类
			List&lt;${table.capitalTableName}Form&gt; ${table.humpTableName}Forms = this.build${table.capitalTableName}Forms(${table.humpTableName}Models);
			
			ResultPage&lt;List&lt;${table.capitalTableName}Form&gt;&gt; resultPage = new ResultPage&lt;List&lt;${table.capitalTableName}Form&gt;&gt;();
			resultPage.setRows(${table.humpTableName}Forms);
			resultPage.setPage(pageIndex);
			resultPage.setSize(pageSize);
			resultPage.setRecords(pageResult.getTotalCount());
			resultEntity.setBusinessObject(resultPage);
			
		} catch (Exception ex) {
			this.logError(MESSAGE_QUERY_FAIL, ex);
			resultEntity.setSuccess(false);
			resultEntity.setMessage(Strings.format("{0}[{1}]", MESSAGE_QUERY_FAIL,ex.getMessage()));
		}
		return resultEntity;
	}

	@Override
	public Result create${table.capitalTableName}(${table.capitalTableName}Form ${table.humpTableName}Form) {
		this.logMethodCalled("create${table.capitalTableName}");
		this.logParamValue("${table.humpTableName}Form", ${table.humpTableName}Form);
		
		Result result = new Result();	
		
		// 参数验证
		if(${table.humpTableName}Form == null){
			result.setSuccess(false);
			result.setMessage(MESSAGE_PARAM_OBJ_NULL);
			return result;
		}
		
		// 构建模型类
		if(Strings.isEmpty(${table.humpTableName}Form.get${pkCol.CapitalColumnName}())){
			${table.humpTableName}Form.set${pkCol.CapitalColumnName}(ObjectHelper.getUUID());
		}
		${table.capitalTableName}Model ${table.humpTableName}Model = new ${table.capitalTableName}Model();
		${table.humpTableName}Form.copyTo(${table.humpTableName}Model);
		${table.humpTableName}Model.setCreateTime(new Date());
		${table.humpTableName}Model.setCreateUserId(VariableFactory.getUserId());
		
		try {
			${table.humpTableName}Model.create();
			result.setMessage(MESSAGE_CREATE_SUCCESS);
		} catch (RecordNotSavedException e) {
			result.setSuccess(false);
			result.setMessage(MESSAGE_CREATE_FAIL);
		}
		return result;
	}

	@Override
	public Result update${table.capitalTableName}(${table.capitalTableName}Form ${table.humpTableName}Form) {
		this.logMethodCalled("update${table.capitalTableName}");
		this.logParamValue("${table.humpTableName}Form", ${table.humpTableName}Form);
		
		boolean isSuccess = true;
		Result result = new Result();	
		
		// 参数验证
		if(${table.humpTableName}Form == null){
			result.setSuccess(false);
			result.setMessage(MESSAGE_PARAM_OBJ_NULL);
			return result;
		}
		
		// 获取用户
		String ${pkCol.columnName} = ${table.humpTableName}Form.get${pkCol.CapitalColumnName}();
		ResultEntity&lt;${table.capitalTableName}Model&gt; resultEntity = this.get${table.capitalTableName}Model(${pkCol.columnName});
		if(!resultEntity.isSuccess()){
			result.setSuccess(false);
			result.setMessage(resultEntity.getMessage());
			result.setStatusCode(resultEntity.getStatusCode());
			return result;
		}
		
		// 设置值
		${table.capitalTableName}Model ${table.humpTableName}Model = resultEntity.getBusinessObject();
		${table.humpTableName}Form.copyTo(${table.humpTableName}Model);
		${table.humpTableName}Model.setUpdateTime(new Date());
		${table.humpTableName}Model.setUpdateUserId(VariableFactory.getUserId());
		
		try {
			${table.humpTableName}Model.update();
			result.setMessage(MESSAGE_UPDATE_SUCCESS);
		} catch (IllegalStateException e1) {
			isSuccess = false;
		} catch (RecordNotSavedException e2) {
			isSuccess = false;
		}

		if(!isSuccess){
			result.setSuccess(false);
			result.setMessage(MESSAGE_UPDATE_FAIL);
		}
		return result;
	}

	@Override
	public ResultEntity&lt;${table.capitalTableName}Form&gt; get${table.capitalTableName}(String ${pkCol.columnName}) {
		this.logMethodCalled("get${table.capitalTableName}");
		this.logParamValue("${pkCol.columnName}", ${pkCol.columnName});
		
		ResultEntity&lt;${table.capitalTableName}Model&gt; resultEntity = this.get${table.capitalTableName}Model(${pkCol.columnName});
		
		ResultEntity&lt;${table.capitalTableName}Form&gt; resultForm = new ResultEntity&lt;${table.capitalTableName}Form&gt;();
		resultForm.setMessage(resultEntity.getMessage());
		resultForm.setStatusCode(resultEntity.getStatusCode());
		
		if(resultEntity.isSuccess()){
			${table.capitalTableName}Form ${table.humpTableName}Form = new ${table.capitalTableName}Form();
			${table.humpTableName}Form.copyFrom(resultEntity.getBusinessObject());
			resultForm.setBusinessObject(${table.humpTableName}Form);
		}else{
			resultForm.setSuccess(false);
		}
		return resultForm;
	}
	
	@Override
	public ResultEntity&lt;${table.capitalTableName}Model&gt; get${table.capitalTableName}Model(String ${pkCol.columnName}) {
		this.logMethodCalled("get${table.capitalTableName}Model");
		this.logParamValue("${pkCol.columnName}", ${pkCol.columnName});
		
		ResultEntity&lt;${table.capitalTableName}Model&gt; resultEntity = new ResultEntity&lt;${table.capitalTableName}Model&gt;();
		
		// 参数验证
		if(Strings.isEmpty(${pkCol.columnName})){
			resultEntity.setSuccess(false);
			resultEntity.setMessage(MESSAGE_PARAM_ID_NULL);
			return resultEntity;
		}
		
		try {
			${table.capitalTableName}Model ${table.humpTableName}Model = ${table.capitalTableName}Model.find(${pkCol.columnName});
			resultEntity.setMessage(MESSAGE_GET_SUCCESS);
			resultEntity.setBusinessObject(${table.humpTableName}Model);
		} catch (RecordNotFoundException e) {
			resultEntity.setSuccess(false);
			resultEntity.setMessage(MESSAGE_GET_FAIL);
		}
		return resultEntity;
	}

	@Override
	public Result delete${table.capitalTableName}(String ${pkCol.columnName}) {
		this.logMethodCalled("delete${table.capitalTableName}");
		this.logParamValue("${pkCol.columnName}", ${pkCol.columnName});
			
		Result result = new Result();	
		
		// 参数验证
		if(Strings.isEmpty(${pkCol.columnName})){
			result.setSuccess(false);
			result.setMessage(MESSAGE_PARAM_ID_NULL);
			return result;
		}
		
		try {
			${table.capitalTableName}Model.delete(${pkCol.columnName});
			result.setMessage(MESSAGE_DELETE_SUCCESS);
		} catch (RecordNotDeletedException e) {
			result.setSuccess(false);
			result.setMessage(MESSAGE_DELETE_FAIL);
		}
		return result;
	}

	@Override
	protected String getTableName() {
		return ${table.capitalTableName}Model.metamodel().getTableName();
	}

	/**
	 * 构建表单类
	 * @param ${table.humpTableName}Models
	 * @return
	 */
	protected List&lt;${table.capitalTableName}Form&gt; build${table.capitalTableName}Forms(List&lt;${table.capitalTableName}Model&gt; ${table.humpTableName}Models) {
		List&lt;${table.capitalTableName}Form&gt; ${table.humpTableName}Forms = new ArrayList&lt;${table.capitalTableName}Form&gt;();
		for (${table.capitalTableName}Model ${table.humpTableName}Model : ${table.humpTableName}Models) {
			${table.capitalTableName}Form ${table.humpTableName}Form = new ${table.capitalTableName}Form();
			${table.humpTableName}Form.copyFrom(${table.humpTableName}Model);
			${table.humpTableName}Forms.add(${table.humpTableName}Form);
		}
		return ${table.humpTableName}Forms;
	}
}
</pre>
